<!DOCTYPE html>
<html>
	<head>
		<title>NerdLuv</title>
		<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
		<link href="heart.gif" type="image/gif" rel="shortcut icon" />
		<link href="nerdluv.css" type="text/css" rel="stylesheet" />
		<?php
			$name = $_REQUEST["name"];
			if(isset($name)==0 ) $error = "Please input the user name"; 
			$gender = $_REQUEST["gender"];
			if( isset($gender) == 0 ) $error = "Please choose your gender";
			$age = $_REQUEST["age"];
			if( isset($age) == 0 ) $error = "Please enter your age!";
			else if( $age <= 0 || $age > 99 ) $error = "Invalid age";
			$personType = $_REQUEST["personType"];
			if( ( $personType[0] != 'I' && $personType[0] != 'E') || ( $personType[1] !='N' && $personType[1] != 'S' )
			    || ($personType[2] != 'F' && $personType[2] != 'T' ) || ( $personType[3] != 'J' && $personType[3] != 'P' ) ) $error = "Invalid personality type!";
			if( isset($personType) == 0 ) $error = "Please enter your personality type"; 
			$os = $_REQUEST["os"];
			if( isset($os) == 0 )  $error = "Please enter your favourite operating system";
			$sexpre = $_REQUEST["sexpre"];
			if( isset($sexpre) == 0 ) $error = "If you don't enter your gender preference, how can I provide a friend for you to date with?"; 
			$lage = $_REQUEST["lage"];
			$uage = $_REQUEST["uage"];
			if( isset($lage) == 0 || isset($uage) == 0 ) $error = "Please enter the age of the people you want to date with";
			if( $lage <= 0 || $lage > 99 || $uage <=0 || $uage > 99 || $lage > $uage) $error = "Invalid ages"; 
			$data = file("singles.txt");
		?>
	</head>

	<body>
		<div id="main">
			<div id="bannerarea">
				<img src="nerdluv.png" alt="banner logo" /> <br />
				where meek geeks meet
			</div>
		
			<div id="matches">
				<h1>Matches for <?=$name?></h1>
			
			<?php
			if( !isset($error) )
			{
				for( $i = 0; $i < count($data); ++$i )
				{
					$mdata = explode(",",$data[$i]);
					if( count($sexpre) == 2 ) $sexpre = $sexpre[0] . $sexpre[1];
					else $sexpre = $sexpre[0];
					?>
					<?php
					if( strlen($sexpre) == 2 && strlen($mdata[5]) == 1 )
					{	
						if($mdata[5] != $gender ) continue;
					}
					else if( strlen($sexpre) == 1 && strlen($mdata[5]) == 2 )
					{
						if ($mdata[1] != $sexpre ) continue;
					}
						
					else if( $sexpre != $mdata[1] || $mdata[5] != $gender )continue;
					$rating = 0;
					if( $mdata[2] >= $lage && $mdata[2] <= $uage ) $rating++;
					if( $mdata[4] == $os ) $rating+=2;
					for( $j=0; $j<4; ++$j)
					{
						if( $mdata[3][$j] == $personType[$j] )
							$rating ++;
					}
					

					if( $rating < 3 ) continue;
			?>
				<div class="match">
					<p class="name">
						<img src="images/<?=strtolower(str_ireplace(" ","_",$mdata[0])).".jpg"?>" alt=<?=$mdata[0]?> />
						<?=$mdata[0]?>
					</p>

					<p class="info">
						<strong>gender:</strong>  <?=$mdata[1]?> <br />
						<strong>age:</strong>     <?=$mdata[2]?> <br />
						<strong>type:</strong>    <?=$mdata[3]?> <br />
						<strong>OS:</strong>      <?=$mdata[4]?> <br />
						<strong>rating:</strong>  <?=$rating?>
					</p>
				</div>
				<?php
				}
				}
				else
				{
				?>
				<h2><?=$error?></h2>
				<?php
				}
				file_put_contents("singles.txt",$name.','.$gender.','.$age.','.$personType.','.$sexpre.'\n',FILE_APPEND);
				
				?>
			</div>
		</div>
		
		<p>
			Results and page (C) Copyright 2009 NerdLuv Inc.
		</p>

		<div id="w3c">
			<a href="http://validator.w3.org/check/referer">
				<img src="http://www.w3.org/Icons/valid-xhtml11" alt="Valid XHTML 1.1" /></a>
			<a href="http://jigsaw.w3.org/css-validator/check/referer">
				<img src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS" /></a>
		</div>
	</body>
</html>
